class AlternateEnding < SystemSymbol

  # Constants
  DA_CAPO = 9
  DAL_SEGNO = 10
  DAL_SEGNO_SEGNO = 11
  NUMBERS_MASK = 0x000007ff
   
  # Constructor
  def initialize(system = DEFAULT_SYSTEM, position = DEFAULT_POSITION, data = DEFAULT_DATA)
    super(system, position, numbers)
  end
   
  # Methods
  def valid_numbers?(numbers)
    return ((numbers == 0) || (((numbers & NUMBERS_MASK) != 0) && ((numbers & ~NUMBERS_MASK) == 0)))
  end
   
  def valid_number?(number)
    return ((number >= 1) && (number <= DAL_SEGNO_SEGNO))
  end
   
  def numbers=(numbers)   
    return false unless valid_numbers?(numbers)
    @data = make_long(0, numbers)
    return true
  end
  
  def numbers()
    return hi_word(@data)
  end
   
  def number=(number)     
    return false unless valid_number(number)
    numbers = numbers()
    numbers |= (2 ** (number - 1))     
    numbers=(numbers)
  end
      
  def number?(number)     
    return false unless valid_number(number)    
    number--
    numbers = numbers()
    power = 2 ** number
    return ((numbers & power) == power)
  end
   
  def number!(number)
    return false unless valid_number(number)
    numbers = numbers()
    numbers &= ~(2 ** (number - 1))
    numbers=(numbers)
    return true
  end
   
  def da_capo
    number= DA_CAPO
  end

  def da_capo?
    return number?(DA_CAPO)
  end

  def da_capo!
    number! DA_CAPO
  end
   
  def dal_segno
    number= DAL_SEGNO
  end
  def dal_segno?
    return number?(DAL_SEGNO)
  end
  def dal_segno!
    number! DAL_SEGNO
  end
   
  def dal_segno_segno
    number= DAL_SEGNO_SEGNO
  end

  def dal_segno_segno?
    return number?(DAL_SEGNO_SEGNO)
  end

  def dal_segno_segno!
    number! DAL_SEGNO_SEGNO
  end
  
  def get_text     
    return_value = ""
     
    group_start = -1
    group_end = -1
     
    first_number = 1
    last_number = 8
    1.upto(8) do |i|
       
      number_set = number?(i)
       
      if (number_set)
        if (group_start == -1)
          group_start = i
          group_end = i
        else
          group_end = i
   	end
      end

      if (i == last_number)
        number_set = false
      end

      if (!number_set && (group_start != -1))
        if (return_value)
          return_value .= ", "
	end
        temp = ""
         
        if (group_start == group_end)
          temp = sprintf("%s.", number_text(group_start))
        elsif (group_start == (group_end - 1))
          temp = sprintf("%s., %s.", number_text(group_start), number_text(group_end))
        else
          temp = sprintf("%s.-%s.", number_text(group_start), number_text(group_end))
        end

        return_value .= temp
         
        group_start = -1
        group_end = -1
      end

    end
    
    DA_CAPO.upto(DAL_SEGNO_SEGNO + 1) do |i|
      if (number?(i))
        if (return_value)
          return_value .= ", "
	end
        return_value .= number_text(i)
      end
    end       

    return return_value
  end
      
  def number_text(number)    
    return false unless valid_number(number)
     
    if (number == DA_CAPO)
      return "D.C."
    elsif (number == DAL_SEGNO)
      return "D.S."
    elsif (number == DAL_SEGNO_SEGNO)
      return "D.S.S."
    end

    return sprintf("%d", number)
  end
   
end
